package com.supreme.proxyserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@RestController
@RequestMapping(value = "json")
public class JsonController {

    @GetMapping(value = "user")
    public AjaxResp getJsonUser() {
        ClassPathResource classPathResource = new ClassPathResource("json/user.json");
        try {
            InputStream inputStream = classPathResource.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String tempString;
            StringBuilder contentString = new StringBuilder();
            while ((tempString = bufferedReader.readLine()) != null) {
                contentString.append(tempString);
                System.out.println(tempString);
            }

            JSONObject jsonObject = JSON.parseObject(contentString.toString());
            return new AjaxResp(jsonObject);
        } catch (IOException e) {
            return new AjaxResp(e.getMessage());
        }
    }
}
